package cn.lijiajia3515.cairo.system.modules.file2;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public interface File2Interface {
	String PREFIX = "/api/file2";

	/**
	 * 获取 访问资源 url
	 *
	 * @param param 参数
	 * @return 已签名 访问资源 url (get)
	 */
	@PostMapping(PREFIX + "/get_objects_url")
	List<String> getObjectsUrl(@RequestBody GetObjectsUrlParam param);

	/**
	 * 获取 访问资源 url
	 *
	 * @param objects 资源值
	 * @return 资源值-访问资源url
	 */
	default Map<String, String> getObjectUrls(List<String> objects) {
		return Optional.ofNullable(objects)
			.filter(x -> !x.isEmpty())
			.map(os -> {
				List<String> urls = getObjectsUrl(GetObjectsUrlParam.builder().keys(objects).build());
				return IntStream.range(0, os.size())
					.mapToObj(x -> Arrays.asList(objects.get(x), urls.get(x)))
					.collect(Collectors.toMap(x -> x.get(0), x -> x.get(1)));
			})
			.orElse(Collections.emptyMap());
	}

	/**
	 * 批量签名
	 *
	 * @param params 签名参数
	 * @return 已签名参数
	 */
	@PostMapping(PREFIX + "/get_upload_objects_sign")
	SignedArgs getUploadObjectsSign(@RequestBody SignFile2Param params);

	/**
	 * 获取上传资源的url
	 *
	 * @param objects objects
	 * @return 上传拿资源 url (put方式上传)
	 */
	@PostMapping(PREFIX + "/get_upload_objects_url")
	List<String> getUploadObjectUrl(@RequestBody List<String> objects);

	/**
	 * 获取上传临时文件 的 url
	 *
	 * @param size size 数量
	 * @return 上传 临时资源 url
	 */
	@PostMapping(PREFIX + "/temporary/get_upload_objects_url")
	List<List<String>> signTemporaryUrl(@RequestParam(name = "Size", defaultValue = "1") Integer size);

	/**
	 * 文件临时上传
	 *
	 * @param files files
	 * @return 文件url
	 */
	@PostMapping(path = PREFIX + "/temporary_upload", produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	List<List<String>> temporaryUpload(@RequestPart(name = "Files") Collection<MultipartFile> files);

	/**
	 * 文件状态 查询
	 *
	 * @param param 参数
	 * @return 1
	 */
	@PostMapping(PREFIX + "/stat/find")
	List<String> findStat(@RequestBody StatFileParam param);

}
